<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Qnr;
use App\Models\QnrCls;
use App\Models\QnrTags;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Table;
use Illuminate\Support\Str;
use Overtrue\Pinyin\Pinyin;

class QnrController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Qnr(),function(Grid $grid) {

            // 禁止
            $grid->toolsWithOutline(false);

            $grid->setActionClass(Grid\Displayers\Actions::class);

            $grid->disableRowSelector();

            $grid->disableBatchDelete();

            // 设置初始排序条件
            $grid->model()->orderBy('id','desc');

            $grid->model()->orderBy('updated_at','desc');

            // 关联
            $grid->model()->with(['qnr_cls']);

            $grid->column('qnr_cls.title','分类');

            // 关联
            $grid->model()->with(['admin_users']);

            $grid->column('admin_users.username','创建用户');

            $grid->column('title')->textarea(true);

            $grid->column('thumbnail')->display(function($pictures) {

                if(empty($pictures)) {
                    $pictures = admin_url() . '/../themes/not.jpg';
                }
                else {

                    $pictures = admin_url() . '/../../../storage/app/public' . $pictures;
                }

                return $pictures;

            })->image();

            $grid->column('is_using')->switch('',true);

            $grid->column('created_at')->display(function($value) {
                return date('Y-m-d h:i',strtotime($value));
            })->sortable();

            $grid->column('updated_at')->display(function($value) {
                return date('Y-m-d h:i',strtotime($value));
            })->sortable();

            $grid->filter(function(Grid\Filter $filter) {
                $filter->equal('title');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id,new Qnr(),function(Show $show) {
            $show->field('c_key');
            $show->field('t_key');
            $show->field('user_id');
            $show->field('title');
            $show->field('content');
            $show->field('introduction');
            $show->field('address');
            $show->field('telephone');
            $show->field('sponsor');
            $show->field('images');
            $show->field('thumbnail');
            $show->field('is_using')->using([1 => '启用',0 => '禁用']);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Qnr(),function(Form $form) {

            $form->disableResetButton();

            $form->disableEditingCheck();

            $form->disableViewCheck();

            $form->hidden('qnr_id');

            $form->hidden('admin_id');

            $form->hidden('slug');

            $form->tab('基础信息',function(Form $form) {

                $form->select('c_key')->placeholder('请选择分类！')
                    ->options(array_slice(QnrCls::selectOptions(),1))
                    ->required()
                    ->rules('required');

                $form->multipleSelect('t_key')->placeholder('请选择标签！')
                    ->options(QnrTags::all()->pluck('name','t_key'))
                    ->saveAsJson();

                $form->text('title')->required()
                    ->rules('required|min:1|max:85|regex:/^[^@!$%]/',[
                        'regex' => '标题存在非法字符！',
                    ]);

                $form->editor('content')->height(800)->required()->rules('required|min:1|max:300000');

                $form->textarea('introduction')->required()->rules('required|min:1|max:3000');

                $form->text('address')->help('例子：广东省广州市白云区鹤龙二路85号地铁3号线永泰站B2出口');

                $form->mobile('tel_phone');

                $form->text('sponsor');

                // 缩略图
                $form->image('thumbnail')
                    ->uniqueName()
                    ->autoUpload()
                    ->disk('public')
                    ->move('/images/qnr')
                    ->maxSize(getConf('IMAGE_UPLOAD_SIZE'))
                    ->accept(getConf('IMAGE_UPLOAD_TYPE'));

                // 图集
                $form->multipleImage('images')
                    ->uniqueName()
                    ->autoUpload()
                    ->disk('public')
                    ->move('/images/qnr')
                    ->maxSize(getConf('IMAGE_UPLOAD_SIZE'))
                    ->accept(getConf('IMAGE_UPLOAD_TYPE'))
                    ->saveAsJson();

                $form->radio('is_top')->options([1 => '开启',0 => '禁用'])->default(0)->required('min:1');

                $form->radio('is_comments')->options([1 => '开启',0 => '禁用'])->default(1)->required('min:1');

                $form->radio('is_using')->options([1 => '开启',0 => '禁用'])->default(1)->required('min:1');

            })->tab('答案与问题',function(Form $form) {

                $form->table('options',function($table) {
                    $table->textarea('key','问题内容');
                    $table->keyValue('value','问题内容')->setKeyLabel('序号')->setValueLabel('答案');;
                    $table->textarea('desc','正确答案');
                })
                    ->saveAsJson();

            });

            // 保存时
            $form->saving(function(Form $form) {

                $form->admin_id = Admin::user()->id;

                if(empty($this->qnr_id)) {
                    $form->qnr_id = Str::uuid();
                }

                // 为空
                if(empty($form->slug)) {
                    $form->slug = Pinyin::permalink($form->title);
                }

            });

        });
    }
}
